package eece261.dChatter;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class dChatter extends Activity {
	
	// For logging.
	private static final String TAG = "dChatter";
	
	// Default username if no name is chosen.
	private static String default_name_ = "freshmeat"; 
	
	// Actual username being used.
	private String	 username_;
	
	// Initial text editing box for typing in a username.
	private EditText edit_text_;
	
	// Method to set the username to the contents of the edit box if there is a name, otherwise sets it to default_name_.
	private void setNameFromBox () {
		
    	username_ = edit_text_.getText().toString();
    	if (username_.length() == 0)
    		username_ = default_name_;	 	
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	Log.d(TAG, "Starting app...");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dchatter);
        
        Log.d(TAG, " Set layout.");
        
    	// Initialize the earlier variables we set up.
    	edit_text_    = (EditText)findViewById(R.id.NameEdit);
    	
    	Log.d(TAG, " Found edit_text_.");
        
        final Button joinGroupButton   = (Button)findViewById(R.id.JoinButton);
        final Button createGroupButton = (Button)findViewById(R.id.CreateGroupButton);
        
        Log.d(TAG, " Found buttons.");
        
        joinGroupButton.setOnClickListener(new View.OnClickListener() {
        	
            public void onClick(View v) {
            	
            	Log.d(TAG, " Setting name.");
            	
            	setNameFromBox();
            	
            	Log.d(TAG, " Username is \"" + username_ + "\".");
            	Log.d(TAG, " Joining active room...");
            	
            	Intent intent = new Intent(dChatter.this, JoinGroup.class);
            	intent.putExtra("username", username_);
            	
            	Log.d(TAG, " Starting JoinGroup.");
            	
            	startActivity(intent);
            }
        });
        
        createGroupButton.setOnClickListener(new View.OnClickListener() {
        	
            public void onClick(View v) {
            	
            	setNameFromBox();
            	Intent intent = new Intent(dChatter.this, ChatWindow.class);
            	intent.putExtra("username", username_);
            	intent.putExtra("ip", "228.6.8.7");
            	intent.putExtra("port", 6759);
            	startActivity(intent);
            }
        });
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	
    }
}